package com.atguigu.yygh.order.controller;

import com.atguigu.yygh.common.R;
import com.atguigu.yygh.enums.OrderStatusEnum;
import com.atguigu.yygh.model.order.OrderInfo;
import com.atguigu.yygh.order.service.OrderService;
import com.atguigu.yygh.utils.AuthContextHolder;
import com.atguigu.yygh.vo.order.OrderCountQueryVo;
import com.atguigu.yygh.vo.order.OrderQueryVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author DongYu Zhou
 * @date 2022/7/7 21:18
 */
@Api(tags = "订单接口")
@RestController
@RequestMapping("/api/order/orderInfo")
public class OrderApiController {

    @Autowired
    private OrderService orderService;

    /**
     * 创建订单
     * @param scheduleId 排班id
     * @param patientId 患者id
     * @return 订单id
     */
    @ApiOperation(value = "创建订单")
    @PostMapping("auth/submitOrder/{scheduleId}/{patientId}")
    public R submitOrder(@PathVariable String scheduleId, @PathVariable Long patientId) {
        Long orderId = orderService.submitOrder(scheduleId, patientId);
        return R.ok().data("orderId", orderId);
    }

    /**
     * 带条件带分页的查询订单
     * @param page
     * @param limit
     * @param orderQueryVo
     * @param request
     * @return
     */
    @ApiOperation(value = "带条件带分页的查询订单")
    @GetMapping("auth/{page}/{limit}")
    public R list(@PathVariable("page") Long page,
                  @PathVariable("limit") Long limit,
                  OrderQueryVo orderQueryVo, HttpServletRequest request){
        //1.获取userid 存到vo中
        Long userId = AuthContextHolder.getUserId(request);
        orderQueryVo.setUserId(userId);
        //2.创建分页查询对象
        Page<OrderInfo> infoPage = new Page<>(page, limit);
        //3.调用接口进行分页查询 b并返回
        Page<OrderInfo> orderInfoPage= orderService.selectPage(infoPage,orderQueryVo);
        return R.ok().data("pageModel",orderInfoPage);
    }

    /**
     * 获取订单状态
     * @return
     */
    @ApiOperation(value = "获取订单状态")
    @GetMapping("auth/getStatusList")
    public R getStatusList() {
        return R.ok().data("statusList", OrderStatusEnum.getStatusList());
    }

    /**
     * 根据id获取订单信息详情
     * @param orderId 订单id
     * @return
     */
    @ApiOperation(value = "根据id获取订单信息详情")
    @GetMapping("auth/getOrders/{orderId}")
    public R getOrders(@PathVariable("orderId") Long orderId){
       OrderInfo orderInfo= orderService.getOrderInfo(orderId);
       return R.ok().data("orderInfo",orderInfo);
    }

    /**
     * 取消预约
     * @param orderId
     * @return
     */
    @ApiOperation(value = "取消预约")
    @GetMapping("auth/cancelOrder/{orderId}")
    public R cancelOrder(
            @PathVariable("orderId") Long orderId) {
        boolean flag = orderService.cancelOrder(orderId);
        return R.ok().data("flag",flag);
    }

    @ApiOperation(value = "获取订单统计数据")
    @PostMapping("inner/getCountMap")
    public Map<String, Object> getCountMap(
            @RequestBody OrderCountQueryVo orderCountQueryVo) {
        Map<String, Object> map =  orderService.getCountMap(orderCountQueryVo);
        return map;
    }





}
